package com.lingyi.algorithm.lab4;

/**
 * @author chenweilong
 * @email 1433471850@qq.com
 * @date 2020-08-20 15:52
 */
public class SqQueue {

    /**
     * 头指针
     */
    private int head;
    /**
     * 尾指针
     */
    private int tail;


    private int size;

    private Integer[] queue;


    public SqQueue(int size) {
        head = 0;
        tail = 0;
        this.queue = new Integer[size];
        this.size = size;
    }


    public void push(Integer data) {
        if (((this.tail + 1) - this.head) % this.size == 0) {
            System.out.println("队列已经满了");
            return;
        }
        this.queue[this.tail] = data;
        if (this.tail + 1 >= this.size) {
            this.tail = 0;
        }else {
            this.tail++;
        }
    }


    public Integer dequeue() {
        if (this.tail == this.head) {
            System.out.println("当前队列为空");
            return null;
        }

        Integer ret = this.queue[this.head];
        if (this.head + 1 >= this.size) {
            this.head = 0;
        }else {
            this.head++;
        }
        return ret;
    }


    public int size() {
        if (this.tail - this.head > 0) {
            return this.tail - this.head;
        }else {
            return (this.tail - this.head) + this.size;
        }
    }


    public static void main(String[] args) {

        SqQueue sqQueue = new SqQueue(3);
        sqQueue.push(1);
        System.out.printf("加入队列之后的长度%s,头指针[%s],尾指针[%s]\n",sqQueue.size(),sqQueue.head,sqQueue.tail);
        sqQueue.push(2);
        System.out.printf("加入队列之后的长度%s,头指针[%s],尾指针[%s]\n",sqQueue.size(),sqQueue.head,sqQueue.tail);
        sqQueue.push(3);
        System.out.printf("加入队列之后的长度%s,头指针[%s],尾指针[%s]\n",sqQueue.size(),sqQueue.head,sqQueue.tail);

        Integer dequeue = sqQueue.dequeue();
        System.out.printf("取出的数字为%s,头指针[%s],尾指针[%s]\n",dequeue,sqQueue.head,sqQueue.tail);
        Integer dequeue1 = sqQueue.dequeue();
        System.out.printf("取出的数字为%s,头指针[%s],尾指针[%s]\n",dequeue1,sqQueue.head,sqQueue.tail);

        Integer dequeue2 = sqQueue.dequeue();
        System.out.printf("取出的数字为%s,头指针[%s],尾指针[%s]\n",dequeue2,sqQueue.head,sqQueue.tail);


        sqQueue.push(1);
        System.out.printf("加入队列之后的长度%s,头指针[%s],尾指针[%s]\n",sqQueue.size(),sqQueue.head,sqQueue.tail);

        sqQueue.push(1);
        System.out.printf("加入队列之后的长度%s,头指针[%s],尾指针[%s]\n",sqQueue.size(),sqQueue.head,sqQueue.tail);


        Integer dequeue3 = sqQueue.dequeue();
        System.out.printf("取出的数字为%s,头指针[%s],尾指针[%s]\n",dequeue3,sqQueue.head,sqQueue.tail);
        Integer dequeue4 = sqQueue.dequeue();
        System.out.printf("取出的数字为%s,头指针[%s],尾指针[%s]\n",dequeue4,sqQueue.head,sqQueue.tail);

        Integer dequeue5 = sqQueue.dequeue();
        System.out.printf("取出的数字为%s,头指针[%s],尾指针[%s]\n",dequeue5,sqQueue.head,sqQueue.tail);


    }


}
